/**
 * 
 */
package net.sf.cldsql.sql2json.utl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.cldsql.dj.utl.DiException;
import net.sf.cldsql.dj.utl.MultipleValueMap;

/**
 * MockHttpServletRequest
 */
public class MockHttpServletRequest implements HttpServletRequest {

    public MultipleValueMap<String, String>  parameterMap = new MultipleValueMap<String, String>();

    public String pathInfo;
    public String method;
    public String authType;
    public MultipleValueMap<String, String> headerMap = new MultipleValueMap<String, String>();
    public Cookie[] cookies;
    public String pathTranslated;
    public String queryString;
    public String remoteUser;
    public String requestURI;
    public StringBuffer requestURL;
    public String requestedSessionId;
    public String servletPath;
    public Principal userPrincipal;
    public boolean requestedSessionIdFromCookie;
    public boolean requestedSessionIdFromURL;
    public boolean requestedSessionIdFromUrl;
    public boolean requestedSessionIdValid;
    public Map<String, Boolean> userInRoleMap = new HashMap<String, Boolean>();
    public MultipleValueMap<String, Object> attributeMap = new MultipleValueMap<String, Object>();
    public String characterEncoding;
    public int contentLength;
    public String contentType;
    public ServletInputStream inputStream;
    public String localAddr;
    public String localName;
    public int localPort;
    public Locale locale;
    public Enumeration locales;

    public String protocol;

    public BufferedReader reader;

    public String realPath;

    public String remoteAddr;

    public String remoteHost;

    public int remotePort;

    public HashMap<String, RequestDispatcher> requestDispatcherMap = new HashMap<String, RequestDispatcher>();

    public String scheme;

    public String serverName;

    public int serverPort;

    public boolean secure;

    public String getAuthType() {
        return this.authType;
    }
    public String getContextPath() {
        return this.getContextPath();
    }
    public Cookie[] getCookies() {
        return this.cookies;
    }
    public long getDateHeader(String name) {
        return parseGmtDateStr(getHeader(name)).getTime();
    }
    public Date parseGmtDateStr(String dateStr) {
        //"Tue, 26 Feb 2013 09:26:57 GMT";  
        final SimpleDateFormat format = new SimpleDateFormat(
                "EEE, dd MMM yyyy HH:mm:ss z", Locale.US);  
        Date date;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            throw new DiException(e);
        }
        return date;
    }
    public String getHeader(String name) {
        return headerMap.get(name);
    }
    public Enumeration getHeaderNames() {
        return new Vector<String>(this.headerMap.keySet()).elements();
    }
    public Enumeration getHeaders(String name) {
        return new Vector<String>(this.headerMap.gets(name)).elements();
    }
    public int getIntHeader(String name) {
        return Integer.parseInt(getHeader(name));
    }
    public String getMethod() {
        return this.method;
    }
    public String getPathInfo() {
        return this.pathInfo;
    }
    public String getPathTranslated() {
        return this.pathTranslated;
    }
    public String getQueryString() {
        return this.queryString;
    }
    public String getRemoteUser() {
        return this.remoteUser;
    }
    public String getRequestURI() {
        return this.requestURI;
    }
    public StringBuffer getRequestURL() {
        return this.requestURL;
    }
    public String getRequestedSessionId() {
        return this.requestedSessionId;
    }
    public String getServletPath() {
        return this.servletPath;
    }
    public HttpSession getSession() {
        return new MockHttpSession();
    }
    public HttpSession getSession(boolean paramBoolean) {
        return new MockHttpSession();
    }
    public Principal getUserPrincipal() {
        return this.userPrincipal;
    }
    public boolean isRequestedSessionIdFromCookie() {
        return this.requestedSessionIdFromCookie;
    }
    public boolean isRequestedSessionIdFromURL() {
        return this.requestedSessionIdFromURL;
    }
    public boolean isRequestedSessionIdFromUrl() {
        return this.requestedSessionIdFromUrl;
    }
    public boolean isRequestedSessionIdValid() {
        return this.requestedSessionIdValid;
    }
    public boolean isUserInRole(String roleName) {
        return this.userInRoleMap.get(roleName);
    }
    public Object getAttribute(String name) {
        List<Object> list = this.attributeMap.gets(name);
        Object ret;
        if (list == null || list.size() == 0) {
            ret = null;
        } else if (list.size() == 1) {
            ret = list.get(0);
        } else {
            ret = list.toArray(new Object[list.size()]);
        }
        return ret;
    }
    public Enumeration getAttributeNames() {
        return new Vector<String>(this.attributeMap.keySet()).elements();
    }
    public String getCharacterEncoding() {
        return this.characterEncoding;
    }
    public int getContentLength() {
        return this.contentLength;
    }
    public String getContentType() {
        return this.contentType;
    }
    public ServletInputStream getInputStream() throws IOException {
        return this.inputStream;
    }
    public String getLocalAddr() {
        return this.localAddr;
    }
    public String getLocalName() {
        return this.localName;
    }
    public int getLocalPort() {
        return this.localPort;
    }
    public Locale getLocale() {
        return this.locale;
    }
    public Enumeration getLocales() {
        return this.locales;
    }
    public String getParameter(String name) {
        return this.parameterMap.get(name);
    }
    public Map getParameterMap() {
        return null;
    }
    public Enumeration getParameterNames() {
        return new Vector<String>(this.parameterMap.keySet()).elements();
    }
    public String[] getParameterValues(String name) {
        List<String> list = this.parameterMap.gets(name);
        return list.toArray(new String[list.size()]);
    }
    public String getProtocol() {
        return this.protocol;
    }
    public BufferedReader getReader() throws IOException {
        return this.reader;
    }
    public String getRealPath(String paramString) {
        return this.realPath;
    }
    public String getRemoteAddr() {
        return this.remoteAddr;
    }
    public String getRemoteHost() {
        return this.remoteHost;
    }
    public int getRemotePort() {
        return this.remotePort;
    }
    public RequestDispatcher getRequestDispatcher(String paramString) {
        return this.requestDispatcherMap.get(paramString);
    }
    public String getScheme() {
        return this.scheme;
    }
    public String getServerName() {
        return this.serverName;
    }
    public int getServerPort() {
        return this.serverPort;
    }
    public boolean isSecure() {
        return this.secure;
    }
    public void removeAttribute(String name) {
        this.attributeMap.remove(name);
    }
    public void setAttribute(String name, final Object object) {
        this.attributeMap.put(name, new ArrayList<Object>() {
            public static final long serialVersionUID = 1L;
        {add(object);}});
    }
    public void setCharacterEncoding(String characterEncoding) throws UnsupportedEncodingException {
        this.characterEncoding = characterEncoding;
    }
}
